/*
 *所有关于sh_progress_xzqdm类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*sh_progress_xzqdmService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using Dapper;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using VOL.AppManager.IRepositories;
using VOL.Core.Extensions;
using VOL.Entity.DomainModels;
using VOL.Entity.新文件夹;

namespace VOL.AppManager.Services
{
    public partial class sh_progress_xzqdmService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Ish_progress_xzqdmRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public sh_progress_xzqdmService(
            Ish_progress_xzqdmRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }


        public PageGridData<shprogressxzqdmdto> GetData(shprogressxzqdmQuery options)
        {
            DynamicParameters sqlParams = new DynamicParameters();
            string sql = " select  row_number() OVER () AS idxx,  * from (   select  * from sh_progress_xzqdm  where 1=1 order by sh_time,xzqdm asc)  t where 1=1  ";
            string sqlcount = "    select  count(1)  from sh_progress_xzqdm where 1=1 ";
            if (options.shzt != null)
            {
                if (options.shzt == -1)
                {
                    sqlcount += " and shzt is null  ";
                    sql += " and shzt is null  ";
                }
                else if (options.shzt == 0)
                {
                    sqlcount += " and shzt='不通过'";
                    sql += "  and shzt='不通过'";
                }
                else if (options.shzt == 1)
                {
                    sqlcount += " and shzt='已通过'";
                    sql += " and shzt='已通过'";
                }
            }
            if (options.xzq != null && options.xzq.Count > 0)
            {
                if (options.xzq.Count == 3)
                {
                    string xzqdm = options.xzq[2].Split('|')[0];
                    sqlcount += " and xzqdm='" + xzqdm + "'  ";
                    sql += "and xzqdm='" + xzqdm + "' ";
                }
                else if (options.xzq.Count == 2)
                {
                    string citydm = options.xzq[1].Split('|')[0];
                    sqlcount += " and citydm='" + citydm + "'  ";
                    sql += "and citydm='" + citydm + "' ";
                }

            }

            sql += "    limit @pagesize offset (@pageindex-1)*@pagesize  ";
            sqlParams.Add("pageindex", options.pageindex);
            sqlParams.Add("pagesize", options.pagesize);
            var datacount = repository.DapperContext.ExecuteScalar(sqlcount, sqlParams).GetInt();
            var data = repository.DapperContext.QueryList<shprogressxzqdmdto>(sql, sqlParams).ToList();
            int totalPages = datacount / options.pagesize;
            if (datacount % options.pagesize != 0)
            {
                totalPages++;
            }
            return new PageGridData<shprogressxzqdmdto> { rows = data, status = 0, total = datacount, summary = new { pagecount = totalPages } };
        }
    }
}
